package com.mentaleye.controller;

import java.util.List;
import java.util.Map;

import com.mentaleye.biz.CommonBizImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.PermissionsBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.PermissionsBiz;
import com.mentaleye.dal.model.PermissionsExample;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping(value="/permissions")
public class PermissionsController {
	
	@Autowired private PermissionsBiz permissionsBiz;
	@Autowired private DictionaryBiz dictionaryBiz;

	@RequestMapping("/permEdit")
	public ModelAndView permEdit(@RequestParam(value = "permissionsId", required = false) Integer permissionsId) {
		ModelAndView view = new ModelAndView("ss/permissions/permissionsEdit6");
		if (permissionsId != null) {
			view.addObject("permissions", permissionsBiz.queryPermissions(permissionsId));
		}
		return view;
	}

	@RequestMapping(value="/permissionsList")
	public ModelAndView list(){
		ModelAndView view=new ModelAndView("ss/permissions/permissionsList");
		Map<Integer, String> permissionsTypeMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.PERMISSIONSTYPE.getCode());
		view.addObject("permissionsTypeMap", permissionsTypeMap);
		view.addObject("permissionsType", JSONObject.toJSONString(permissionsTypeMap));
		return view;
	}
	
	@RequestMapping(value="/permissionsListData")
	@ResponseBody
	public String permissionsListDate(CommonPage commonPage){
		permissionsBiz.queryPermissions(commonPage);
		return commonPage.toSHowPage();
	}
	
	@RequestMapping(value="/treeData")
	@ResponseBody
	public  String listdata(){
		/*permissionsBiz.queryPermissions(commonPage);
		return commonPage.toSHowPage();*/
		return JSONObject.toJSONString(permissionsBiz.queryPermissions(new PermissionsExample()));
	}
	
	@RequestMapping(value="/save")
	public @ResponseBody String save(PermissionsBean permissionsBean){
		return JSONObject.toJSONString(permissionsBiz.editPermissions(permissionsBean));
	}
	@RequestMapping(value="/doPermEdit")
	public @ResponseBody String doPermEdit(PermissionsBean permissionsBean){
		return JSONObject.toJSONString(CommonBizImpl.edit(permissionsBean,"Permissions","permissionsId",null));
	}
	
	 @RequestMapping(value = "/delete")
	    public @ResponseBody String delete(@RequestBody  List<Integer> permissionsId) {
	        return JSONObject.toJSONString(permissionsBiz.deletePermissions(permissionsId));
	    }
	 @RequestMapping(value = "/permissionsEdit")
		public ModelAndView studentsEdit(@RequestParam(value = "id", required = false) Integer permissionsId) {
			ModelAndView view = new ModelAndView("ss/permissions/permissionsEdit");
			
			Map<Integer, String> permissionsTypeMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.PERMISSIONSTYPE.getCode());
			view.addObject("permissionsTypeMap", permissionsTypeMap);
			view.addObject("permissionsType", JSONObject.toJSONString(permissionsTypeMap));
			PermissionsExample example=new PermissionsExample();
			example.createCriteria().andPermissionsTypeEqualTo(84);
			view.addObject("permissionses",permissionsBiz.queryPermissions(example));
			if (permissionsId != null) {
				view.addObject("permissions",permissionsBiz.queryPermissions(permissionsId) );
				
				
				
			}
			return view;
		}
	 
	 @RequestMapping(value = "/permissionsEdits")
		public ModelAndView studentsEdits(@RequestParam(value = "id", required = false) Integer permissionsId) {
			ModelAndView view = new ModelAndView("ss/permissions/permissionsEdit2");
			
			Map<Integer, String> permissionsTypeMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.PERMISSIONSTYPE.getCode());
			view.addObject("permissionsTypeMap", permissionsTypeMap);
			view.addObject("permissionsType", JSONObject.toJSONString(permissionsTypeMap));
			PermissionsExample example=new PermissionsExample();
			example.createCriteria().andPermissionsTypeEqualTo(84);
			view.addObject("permissionses",permissionsBiz.queryPermissions(example));
			if (permissionsId != null) {
				view.addObject("permissions",permissionsBiz.queryPermissions(permissionsId) );
			}
			return view;
		}
	 
	 
	 @RequestMapping(value = "/permissionsEditses")
		public ModelAndView studentsEditses(@RequestParam(value = "id", required = false) Integer permissionsId) {
			ModelAndView view = new ModelAndView("ss/permissions/permissionsEdit3");
			
			Map<Integer, String> permissionsTypeMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.PERMISSIONSTYPE.getCode());
			view.addObject("permissionsTypeMap", permissionsTypeMap);
			view.addObject("permissionsType", JSONObject.toJSONString(permissionsTypeMap));
			PermissionsExample example=new PermissionsExample();
			example.createCriteria().andPermissionsTypeEqualTo(85);
			view.addObject("permissionses",permissionsBiz.queryPermissions(example));
			if (permissionsId != null) {
				view.addObject("permissions",permissionsBiz.queryPermissions(permissionsId) );
			}
			return view;
		}
	 
	
	
	

}
